@{
    ViewData["Title"] = "添加入库明细";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>@ViewData["Title"]</title>
    <link rel="stylesheet" href="~/layui/css/layui.css">
</head>
<body>
    <div style="padding: 20px;">
        <form class="layui-form" lay-filter="detailForm">
            <input type="hidden" name="procurementId" value="@ViewBag.ProcurementId">
            
            <div class="layui-form-item">
                <label class="layui-form-label">物料编码</label>
                <div class="layui-input-block">
                    <div class="layui-input-group">
                        <input type="text" name="materialCode" placeholder="请选择物料" class="layui-input" readonly lay-verify="required">
                        <input type="hidden" name="materialId">
                        <div class="layui-input-split layui-input-suffix" style="cursor: pointer;" id="btnSelectMaterial">
                            <i class="layui-icon layui-icon-search"></i>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">物料名称</label>
                <div class="layui-input-block">
                    <input type="text" name="materialName" class="layui-input" readonly>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                    <select name="warehouseId" lay-verify="required" lay-search="">
                        <option value="">请选择仓库</option>
                    </select>
                </div>
            </div>

            <div class="layui-row layui-col-space10">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="inputNum" placeholder="请输入入库数量" class="layui-input" lay-verify="required|number">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">金额</label>
                        <div class="layui-input-block">
                            <input type="number" name="money" placeholder="请输入金额" class="layui-input" lay-verify="number">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">批次</label>
                        <div class="layui-input-block">
                            <input type="text" name="batch" placeholder="请输入批次" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">入库日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="inDate" placeholder="请选择入库日期" class="layui-input" id="inDate" lay-verify="required">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <textarea name="desc" placeholder="请输入描述" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveDetail">保存</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="parent.layer.closeAll()">取消</button>
                </div>
            </div>
        </form>
    </div>

    <script src="~/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer'], function () {
            var $ = layui.jquery;
            var form = layui.form;
            var laydate = layui.laydate;
            var layer = layui.layer;

            // 初始化日期选择器
            laydate.render({
                elem: '#inDate',
                type: 'datetime'
            });

            // 设置默认日期
            $('#inDate').val(formatDateTime(new Date()));

            // 加载仓库选择列表
            loadWarehouseSelect();

            // 选择物料按钮
            $('#btnSelectMaterial').on('click', function() {
                selectMaterial();
            });

            // 保存明细
            form.on('submit(saveDetail)', function(data) {
                var field = data.field;
                
                // 验证必填字段
                if (!field.materialId) {
                    layer.msg('请选择物料');
                    return false;
                }
                
                if (!field.warehouseId) {
                    layer.msg('请选择仓库');
                    return false;
                }

                $.post('/Procurement/AddDetail', field, function(result) {
                    if (result.success) {
                        layer.msg('添加成功');
                        setTimeout(function() {
                            parent.layer.closeAll();
                        }, 1000);
                    } else {
                        layer.msg(result.message || '添加失败');
                    }
                }).fail(function(xhr, status, error) {
                    layer.msg('请求失败，请检查网络连接');
                });
                return false;
            });

            // 加载仓库选择列表
            function loadWarehouseSelect() {
                $.get('/Procurement/GetWarehouseSelect', function(result) {
                    if (result.success && result.data) {
                        var select = $('select[name="warehouseId"]');
                        select.empty().append('<option value="">请选择仓库</option>');
                        $.each(result.data, function(index, item) {
                            select.append('<option value="' + item.id + '">' + item.warehouseName + '</option>');
                        });
                        // 重新渲染select
                        form.render('select');
                    }
                });
            }

            // 选择物料
            function selectMaterial() {
                // 设置回调函数
                window.selectMaterialCallback = function(selectedData) {
                    $('input[name="materialId"]').val(selectedData.id);
                    $('input[name="materialCode"]').val(selectedData.materielCode);
                    $('input[name="materialName"]').val(selectedData.materielName);
                };

                layer.open({
                    type: 2,
                    title: '选择物料',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['800px', '600px'],
                    content: '/Procurement/MaterialSelect'
                });
            }

            // 格式化日期时间
            function formatDateTime(date) {
                return date.getFullYear() + '-' +
                    ('0' + (date.getMonth() + 1)).slice(-2) + '-' +
                    ('0' + date.getDate()).slice(-2) + ' ' +
                    ('0' + date.getHours()).slice(-2) + ':' +
                    ('0' + date.getMinutes()).slice(-2) + ':' +
                    ('0' + date.getSeconds()).slice(-2);
            }
        });
    </script>
</body>
</html> 